import * as types from "../action-types";
import { getToken } from "@/utils/auth";
const initUserInfo = {
  userid: '',
  nickName: "",
  realName:"",
  role: null,
  status: 0, // 状态： 0-正常 1-冻结 2-注 销
  code: '', // 用户唯一code
  avatar: "",
  registerTime: '', // 注册时间
  token: getToken(),
};
export default function user(state = initUserInfo, action) {
  switch (action.type) {
    case types.USER_SET_USER_TOKEN:
      return {
        ...state,
        token: action.token,
      };
    case types.USER_SET_USER_INFO:
      return {
        ...state,
        nickName: action.nickName,
        realName: action.realName,
        avatar: action.avatar,
        code: action.code,
        userid: action.id,
        status: action.status,
        registerTime: action.registerTime
      };
    case types.USER_SET_USER_PERMISSIONS:
      return {
        ...state,
        role: action.permissions,
      };
    case types.USER_RESET_USER:
      return {};
    default:
      const permissions = localStorage.getItem('permissions'), localPermissions = permissions ? JSON.parse(permissions).permissions : null;
      return {
        ...state,
        role: action.permissions || localPermissions,
      };
  }
}
